package com.warren.blog.testkafaka;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;

/**
 * @author warren
 * @data: 2022/3/29 10:30 PM
 * @decription:
 */
public class CustomProducerAcks {
    public static void main(String[] args) {
        //0 配置
        Properties properties = new Properties();
        //连接kafka集群
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.1.9:9092");
        //序列化
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        // acks
        properties.put(ProducerConfig.ACKS_CONFIG, "1");
        // 重试三次
        properties.put(ProducerConfig.RETRY_BACKOFF_MS_CONFIG, 3);

        //创建生产者
        KafkaProducer<String, String> kafkaProducer = new KafkaProducer<>(properties);

        //发送数据
        for (int i = 0; i < 5; i++) {
            kafkaProducer.send(new ProducerRecord<>("test1", "warren" + i * 3));
        }

        //关闭资源
        kafkaProducer.close();
    }
}
